package com.zhao.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Chart;
import com.zhao.pojo.User;
import com.zhao.service.ChartService;
import com.zhao.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    RoleService roleService;
    @Autowired
    ChartService chartService;

    @RequestMapping("/articleCollect")
    public String articleCollect(@RequestParam(required = false)String groupname, Model model){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> groups = chartService.CollectgroupAll(loginUser.getUserAccount());
        System.out.println(groups);
        if (groups.size()>0){
            List<Chart> charts=new ArrayList<>();
            if (groupname==null){
                charts=chartService.CollectByAll(loginUser.getUserAccount(),groups.get(0));
                model.addAttribute("groupname",groups.get(0));
            }else {
                charts=chartService.CollectByAll(loginUser.getUserAccount(),groupname);
                model.addAttribute("groupname",groupname);
            }
            model.addAttribute("charts",charts);
        }
        model.addAttribute("groups",groups);
        return "admin/articleCollect";
    }
    //@MyLog(value = "修改收藏夹内容")
    @RequestMapping("/updateCollect/{groupname}")
    public String updateCollect(@PathVariable String groupname,Model model){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<Chart> charts = chartService.CollectByAll(loginUser.getUserAccount(), groupname);
        List<Map<String,String>> list=new ArrayList<>();
        Map<String,String> map;
        for (Chart chart:charts) {
            map=new HashMap<>();
            map.put("value",chart.getId()+"");
            map.put("title",chart.getTitle());
            list.add(map);
        }
        List<String> groups = chartService.CollectgroupAll(loginUser.getUserAccount());
        model.addAttribute("groups",groups);
        model.addAttribute("oldName",groupname);
        model.addAttribute("data",list);
        return "admin/updateCollect";
    }
}
